package com.jd.cs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jd.cs.entity.UserPo;
import com.jd.cs.service.UserService;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lizhiwei
 * @since 2020-06-22
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    /**
     * 分页查询数据
     *
     * @return
     */
    @GetMapping("/select_paged")
    public IPage<UserPo> selectPaged(int pageNum, int pageSize) {
        Page<UserPo> page2 = new Page<>(pageNum, pageSize);
        userService.page(page2, null);
        return page2;
    }


    /**
     * 分页查询数据
     *
     * @return
     */
    @GetMapping("/select2")
    public UserPo select2(int id) {
        return userService.select2(id);
    }

    /**
     * 分页查询数据
     *
     * @return
     */
    @GetMapping("/select3")
    public UserPo select3(int id) {
        return userService.select3(id);
    }

    /**
     * 分页查询数据
     *
     * @return
     */
    @GetMapping("/updatePassword")
    public int updatePassword(int id, String password) {
        logger.info("updatePassword id={},password={}", id, password);
        return userService.updatePassword(id, password);
    }

    /**
     * 分页查询数据
     *
     * @return
     */
    @PostMapping("/postTest")
    public PostTestRo postTest(@Valid PostTestRo vo) {
        return vo;
    }
}

@Data
class PostTestRo {
    @Min(10)
    private int id;
    @NotBlank
    private String password;
}